#NoTrayIcon

#region ### Check Files ###
FileDelete("Map_Cache\")
DirCreate("Map_Cache")
If FileExists("errorlog.log") Then FileDelete("errorlog.log")
If FileExists("maplog.log") Then FileDelete("maplog.log")
If Not FileExists("data\settings\settings.dat") Then _Write("Cant find settings file!", 1)
#endregion ### Check Files ###

OnAutoItExitRegister("_Exit")

Global Const $GAME_DEBUG = True
Global Const $GAME_VERS = "0.1.0_1";DONT CHANGE!

_Write("Started WISH")
_Write("Version : " & $GAME_VERS)

Global Const $GAME_CURSOR = _Setting_Load("GAME_Cursor")

AdlibRegister("_Screenshot")


;###################################################INCLUDES###################################################
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <ScreenCapture.au3>
#include <Sound.au3>
#include <String.au3>
#include <File.au3>
#include <ComboConstants.au3>
#include 'udf\Console.au3'
;#################################################INCLUDES-END##################################################



#region ### Variablen ###
$hGUI = GUICreate("WISH", 800, 600, -1, -1, $WS_POPUP);create the game window

If $GAME_CURSOR = 1 Then GUISetCursor(16, 1, $hGUI);hides the normal windows cursor.
Global $bIsConnected = False

TCPStartup()

_GDIPlus_Startup();start gdi+

Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI);the graphics for screen
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics(800, 600, $hGraphics);bitmap
Global $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap);a buffer. If you wanna draw, then do it HERE!

Global $hMapBitmap = _GDIPlus_BitmapCreateFromGraphics(600, 600, $hGraphics)
Global $hMapBuffer = _GDIPlus_ImageGetGraphicsContext($hMapBitmap)

Global $hCursorImage = _GDIPlus_ImageLoadFromFile("pictures\gui\mouse.png");The Image for the cursor I want to use.
Global $hWallpaperImage = _GDIPlus_ImageLoadFromFile("pictures\gui\start_screen.png");loads the start picture
Global $hGroundImage = _GDIPlus_ImageLoadFromFile("pictures\map_pictures\pokemon sprites.bmp")
Global $hButtonImage = _GDIPlus_ImageLoadFromFile("pictures\gui\button_brown.png")
Global $hCharactersImage = _GDIPlus_ImageLoadFromFile("pictures\sprites\npcs.png")
Global $hJoinBattleButton = _GDIPlus_ImageLoadFromFile("pictures\gui\Button_JoinBattle.png")
Global $hMessagesButton = _GDIPlus_ImageLoadFromFile("pictures\gui\Button_Msgs.png")
Global $hSpellShortcutImage = _GDIPlus_ImageLoadFromFile("pictures\gui\spell_shortcut.png")
Global $hWindowImage = _GDIPlus_ImageLoadFromFile("pictures\gui\window.png")

Global $hWhiteBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF);What could a white brush be?
Global $hButtonBrush = _GDIPlus_BrushCreateSolid(0xFF012600);The background color for my buttons... I will not use them in the final version...

Global $hMailIcon = _GDIPlus_ImageLoadFromFile("res\icons\mail.png")



If @Compiled Then
	Global $SERVER_IP = FileReadLine("data\BlackCon\server.dat", 1)
	Global $SERVER_Port = FileReadLine("data\BlackCon\server.dat", 2)
Else
	Global $SERVER_IP = "127.0.0.1"
	Global $SERVER_Port = 33891
EndIf

Global $CLIENT_IP = '127.0.0.1'; this is for older versions... you dont need thsi but dont delete it!
Global $CLIENT_Port = 33891

Global $sndTitleMusic = _SoundOpen("sounds\music_1.mp3")

Global Const $MAP_W = 16
Global Const $MAP_H = 16

;Interface Vars#######################################################################
Global $iSpellboardSize = _Setting_Load("INTERFACE_spellboard", 50)
;#####################################################################################

_SoundPlay($sndTitleMusic)


If _Setting_Load("MOUSE_trap") = 1 Then
	$aCoords = WinGetPos($hGUI)
	_MouseTrap($aCoords[0], $aCoords[1], $aCoords[0] + $aCoords[2] - 4, $aCoords[1] + $aCoords[3] - 4)
EndIf

Global $sServerNews = ""

#endregion ### Variablen ###

If $GAME_DEBUG Then _ConsoleSetTitle("Debugging")
;~ _Write("This game is using the 'Console.au3' udf by Gunnar (Marthog)" & @CRLF)

_BlackCon_LoginScreen()

#region ### UDF ###
#include 'udf\gui.au3'
#include 'udf\mouse.au3';the udf for mouse control, painting, etc.
#include 'udf\BlackCon.au3'
#include 'udf\game.au3'
#include 'udf\start_menu.au3'
#include 'udf\map.au3'
#include 'udf\message.au3'
#include 'udf\settings.au3'
#endregion ### UDF ###


Func _Exit($bla = 1)
	_Write("_Exit ()")
	If $bIsConnected Then _BlackCon_SendPacket("ext|")

	_GDIPlus_BrushDispose($hButtonBrush)
	_GDIPlus_BrushDispose($hWhiteBrush)

	_GDIPlus_BitmapDispose($hBitmap)
	_GDIPlus_GraphicsDispose($hGraphics)

	_GDIPlus_BitmapDispose($hMapBitmap)

	_GDIPlus_ImageDispose($hCursorImage)
	_GDIPlus_ImageDispose($hWallpaperImage)
	_GDIPlus_ImageDispose($hMailIcon)
	_GDIPlus_ImageDispose($hMessagesButton)
	_GDIPlus_ImageDispose($hJoinBattleButton)
	_GDIPlus_ImageDispose($hCharactersImage)
	_GDIPlus_ImageDispose($hButtonImage)
	_GDIPlus_ImageDispose($hGroundImage)

	_GDIPlus_Shutdown()


	_BlackCon_Shutdown()
	TCPShutdown()

	_Setting_Save("GAME_Cursor", $GAME_CURSOR)
	_Setting_Save("INTERFACE_spellboard", $iSpellboardSize)

	If Not FileExists("logfiles\") Then DirCreate("logfiles\")

	If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
	$iMsgBoxAnswer = MsgBox(260,"","Do you want to create an error logfile?" & @CRLF & "You can give it to the development team, so they can find a bug." & @CRLF & @CRLF & "Create logfile?",5)
	Select
		Case $iMsgBoxAnswer = 6 ;Yes
			FileMove("errorlog.log", "logfiles\errorlog_" & @MDAY & "." & @MON & "." & @YEAR & "_" & @HOUR & "." & @MIN & "." & @SEC & ".log")
		Case $iMsgBoxAnswer = 7 ;No
			FileDelete("errorlog.log")
		Case $iMsgBoxAnswer = -1 ;Timeout
			FileDelete("errorlog.log")
	EndSelect

	Exit
EndFunc   ;==>_Exit

Func _Write($sText, $iError = 0, $iMapLog = 0)
	Switch $iError
		Case 1
			_ConsoleSetColor($_CONSOLE_RED)
			$sText = "[ERROR]" & $sText
		Case 2
			_ConsoleSetColor($_CONSOLE_BLUE)
			$sText = "[FAILURE]" & $sText
	EndSwitch

	$sText = "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]" & " " & $sText

	If $GAME_DEBUG Then
		ConsoleWrite($sText & @CRLF)
	EndIf

	Switch $iMapLog
		Case 0
			FileWrite("errorlog.log", $sText & @CRLF)
		Case 1
			FileWrite("maplog.log", $sText & @CRLF)
	EndSwitch


	_ConsoleSetColor(7, 0)
EndFunc   ;==>_Write

Func _Screenshot()
	If _IsPressed(11) And _IsPressed(53) Then
		_Write("_Screenshot ()")
		If Not FileExists("screenshots\") Then DirCreate("screenshots\")
		_ScreenCapture_CaptureWnd(@ScriptDir & "\screenshots\scr-" & Random(1, 10000, 1) & ".png", $hGUI)
	EndIf
EndFunc   ;==>_Screenshot

Func _Addon_LoadAddons()
	_Write("_Addon_LoadAddons ()")
	$aAddons = _FileListToArray("addons\", "*.wsa")
	For $i = 1 To $aAddons[0]
		_Write("Load Addon & " & $aAddons[$i])
	Next
EndFunc   ;==>_Addon_LoadAddons

